#include "Core\Cardinal.h"
#include <cassert>
#include <exception>
#include <fstream>
#include <iostream>
#include <string>
#include "Component\Physics\Transform.h"
#include "Component\Renderer\Base\RendererControl.h"
#include "Core\Item.h"
#include "Includes\SDefine.h"

namespace SakuraAge {
void SakuraAge::Cardinal::Update() {
  for (auto i : ItemFactor::list) {
    for (auto j : i->components) {
      try {
        j->Update();
        //throw std::exception("ERROR");
      } catch (std::exception& error) {
        DebugLogError(std::string("[RuntimeError] ") + error.what() +
                      "\nin Object" + i->name + " in Component " +
                      typeid(*j).name() + " in function Update()\n");
      }
    }
  }
  for (auto i : RendererControl::renderers) {
    try {
      i->OnRenderer();
    } catch (std::exception& error) {
      DebugLogError(std::string("[RuntimeError] ") + error.what() +
                    "\nin Renderer " + typeid(i).name() +
                    " in function OnRenderer()\n");
    }
  }
}

/*const std::string SakuraAge::Cardinal::LoadString(const std::string& path) {
  std::ifstream a;
  a.open(path.c_str(), std::ios_base::in);
  std::string ret;
  while (!a.eof()) {
    ret.push_back(a.get());
  }
  a.close();
  return ret;
}*/

void SakuraAge::Cardinal::SetActive(size_t index) {
  scenes[activeScene]->Father->active = false;
  activeScene = index;
  scenes[index]->Father->active = true;
}

std::vector<ObjRef<SakuraAge::Scene>> SakuraAge::Cardinal::scenes;

size_t SakuraAge::Cardinal::activeScene = 0;
}  // namespace SakuraAge
